package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.adapter.SZJAdapter
import com.example.xinhuazidian.bean.TSSBS
import com.example.xinhuazidian.databinding.ActivityTangshisanbaiXiangqingBinding
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * 唐诗三百首详情
 */
class TangshisanbaiXiangqingActivity : AppCompatActivity() {

    private lateinit var binding: ActivityTangshisanbaiXiangqingBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTangshisanbaiXiangqingBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
        initData()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        val beanStr = intent.getStringExtra("bean")
        if (beanStr != null) {
            val bean = Gson().fromJson(beanStr, TSSBS.ContentBeanX.ContentBean::class.java)

            binding.tvTitle.text = "${bean.chapter} · ${bean.author}"
            initAdapter(bean.paragraphs)
        }
    }

    private fun initAdapter(list: MutableList<String>) {
        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL)
        binding.rv.layoutManager = staggeredGridLayoutManager

        //加载适配器
        val szjAdapter = SZJAdapter(list)
        binding.rv.adapter = szjAdapter
    }
}